package com.aikg.kgrag_java.service.impl;

import com.aikg.kgrag_java.pojo.request.EmbeddingDocsRequestBody;
import com.aikg.kgrag_java.pojo.request.EmbeddingQueryRequestBody;
import com.aikg.kgrag_java.pojo.response.EmbeddingDocsResponse;
import com.aikg.kgrag_java.pojo.response.EmbeddingQueryResponse;
import com.aikg.kgrag_java.service.IEmbeddingService;
import com.aikg.kgrag_java.utils.JsonUtils;
import com.aikg.kgrag_java.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: EmbeddingServiceImpl
 * @Description: 调用embedding模型接口的服务实现类
 * @Author: 李宗雨
 * @Date: 2024/12/26 17:41
 * @Version: 1.0
 **/
@Service
@PropertySource({"classpath:application.yml" })
public class EmbeddingServiceImpl implements IEmbeddingService {

    @Value("${pythonApi.url}")
    private String url;

    @Value("${pythonApi.port}")
    private String port;

    @Override
    public List<Double> embeddingQuery(String query) {
        EmbeddingQueryRequestBody embeddingQueryRequestBody = new EmbeddingQueryRequestBody();
        embeddingQueryRequestBody.setQuery(query);
        EmbeddingQueryResponse response = JsonUtils.toObj(
                UrlUtils.httpRequest(url + ":" + port + "/embeddingdoc", embeddingQueryRequestBody),
                EmbeddingQueryResponse.class
        );
        if(response.getCode() == 200){
            // 成功
            return response.getData();
        }
        return null;
    }


    @Override
    public List<List<Double>> embeddingDocs(List<String> docs) {
        EmbeddingDocsRequestBody embeddingDocsRequestBody = new EmbeddingDocsRequestBody();
        embeddingDocsRequestBody.setDocs(docs);
        EmbeddingDocsResponse response = JsonUtils.toObj(
                UrlUtils.httpRequest(url + ":" + port + "/embeddingdocs", embeddingDocsRequestBody),
                EmbeddingDocsResponse.class
        );
        if(response.getCode() == 200){
            // 成功
            return response.getData();
        }
        return null;
    }
}
